package cn.gdchent.springbootsecurity.service.impl;

import cn.gdchent.springbootsecurity.enums.ResultStatusEnum;
import cn.gdchent.springbootsecurity.form.LoginForm;
import cn.gdchent.springbootsecurity.response.HttpResultMap;
import cn.gdchent.springbootsecurity.service.LoginService;
import cn.gdchent.springbootsecurity.utils.JwtTokenUtil;
import cn.gdchent.springbootsecurity.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private JwtTokenUtil JwtTokenUtil;

    @Qualifier(value = "userDetailsServiceImpl")
    @Autowired
    private UserDetailsService userDetailsService;

    /**
     * 基于前后端分离情况使用spring security来进行认证授权
     *
     * @param loginForm
     * @return
     */
    @Override
    public HttpResultMap login(LoginForm loginForm) {
        String username = loginForm.getUserName();
        String password = loginForm.getPassword();
        try {
            if (StringUtils.isEmpty(username)) {
                System.out.println("用户名不能为空");
                throw new RuntimeException("用户名不能为空");
            }
            //Collection<GrantedAuthority> authorityCollection=new ArrayList<>();
            //authorityCollection.add(new SimpleGrantedAuthority("ROLE_USER"));
            UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
                    username,
                    password
            );
            Authentication authentication = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
            //自己设置到securityContextHolder容器中去
            SecurityContextHolder.getContext().setAuthentication(authentication);

        } catch (AuthenticationException e) {
            throw new RuntimeException(e.getMessage());
        }
        //生成jwt
        HttpResultMap httpResultMap = new HttpResultMap();
        JSONObject data = new JSONObject();
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        String token = JwtTokenUtil.generateToken(userDetails);
        data.put("token", token);
        httpResultMap.put(HttpResultMap.CODE_TAG, ResultStatusEnum.SUCCESS.getStatus());
        httpResultMap.put(HttpResultMap.MSG_TAG, ResultStatusEnum.SUCCESS.getMsg());
        httpResultMap.put(HttpResultMap.DATA_TAG, data);
        return httpResultMap;
    }

}
